<template>
	<view class="content">
		<u-form ref="uForm" label-width="200" class="formStyle">
			<view class="oneCard" style="border: none;">
				<!-- 	<view class="line-view"><uni-data-select v-model="factoryId" :localdata="factorieList"
						placeholder="选择工厂" @change="factoryIdChange"></uni-data-select></view> -->
				<view class="line-view" style="align-items: center;">
					<u-input border v-model="palletCode" :inputBorder="false" :focus="palletFocus"
						@focus="palletCode = ''" placeholder="扫描托盘码" @confirm="palletConfirm" />
					<text class="addText" @click="addPallet">生成</text>
				</view>
				<view class="line-view">
					<u-input border v-model="boxCode" :inputBorder="false" :focus="boxFocus" @focus="boxCode = ''"
						ref="boxInput" placeholder="扫描箱码" @confirm="boxConfirm" />
				</view>
			</view>
			<view class="flexFull" style="margin-top: 15rpx;">
				<view v-for="(item, index) in formModel" :key="index" class="oneCard">
					<view class="line-view">
						<view class="line-text">箱码</view>
						<view class="line-value">
							{{item.barcode}}
						</view>
					</view>
					<view class="line-view">
						<view class="line-text">可用数量</view>
						<view class="line-value">
							{{ item.availableQuality }}
						</view>
					</view>
				</view>
			</view>
		</u-form>
		<!-- <view class="btnRow"><u-button type="primary" size="medium" @click="subAll">移库上架</u-button></view> -->

		<view class="btn-bottom-view-pallet" style="padding: 25rpx;"><button type="primary" @click="subAll">组托 /
				拆托</button></view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	var that;
	export default {
		data() {
			return {
				productInfo: {},
				palletCode: null,
				boxCode: null,
				stockInfo: {},
				factoryId: null,
				factorieList: [],
				palletFocus: true, //托盘码焦点
				boxFocus: false, //箱码焦点
				scanSearchOut: '',
				scanSearchIn: '',
				inObj: {},
				goodsCode: '',
				formModel: [],
				oldFactoryId: null //保存的id
			};
		},
		onLoad() {
			that = this;
			// that.getList();
		},
		methods: {
			setBoxFocus() {
				that.boxFocus = false
				this.$nextTick(function() {
					setTimeout(function() {
						that.boxFocus = true
					}, 100);
				})
			},
			// 扫描托盘码
			palletConfirm(e) {
				that.$u.toast('扫描成功')

				that.setBoxFocus()

				// if (!that.factoryId) {
				// 	this.$refs.uToast.show({
				// 		title: '请选择工厂',
				// 		type: 'error'
				// 	});
				// 	return;
				// }

				// this.$u.post('/wms/mobile/api/getPalletCode', {
				// 	palletCode: e
				// }).then(res => {
				// });
			},
			// 扫描箱码
			boxConfirm(e) {
				// if (!that.factoryId) {
				// 	this.$refs.uToast.show({
				// 		title: '请选择工厂',
				// 		type: 'error'
				// 	});
				// 	return;
				// }
				// if (!that.stockInfo.id) {
				// 	this.$refs.uToast.show({
				// 		title: '请扫描仓位',
				// 		type: 'error'
				// 	});
				// 	return;
				// }
				if (!e) {
					return false
				}
				for (const item of that.formModel) {
					if (e === item.cartonNo) {
						that.$u.toast('该箱码已存在')
						that.setBoxFocus()
						return false
					}
				}
				this.$u.post('/wms/mobile/api/getArrayByCartonCode', {
					cartonNo: e
				}).then(res => {
					console.log(res);
					const list = that.$u.deepClone(that.formModel)
					list.push(res.data[0])
					that.formModel = list
					that.setBoxFocus()
				}).catch(err => {
					that.setBoxFocus()
				});
			},
			// 生成托盘码
			addPallet() {
				this.$u.post('/wms/mobile/api/createPalletCode', {}).then(res => {
					console.log(res);
					that.palletCode = res.data
					that.setBoxFocus()
				})
			},
			// factoryIdChange(e) {
			// 	if (e != that.oldFactoryId && that.formModel.length > 0) {
			// 		uni.showModal({
			// 			title: '提示',
			// 			content: '选择工厂会清空已扫描内容,是否继续?',
			// 			success: function(res) {
			// 				if (res.confirm) {
			// 					// 点确定
			// 					that.oldFactoryId = e;
			// 					that.formModel = [];
			// 					that.stockInfo = [];
			// 					that.palletCode = null;
			// 					that.palletFocus = true;
			// 				} else if (res.cancel) {
			// 					// 点取消
			// 					setTimeout(function() {
			// 						that.factoryId = that.oldFactoryId;
			// 						that.palletFocus = true;
			// 					}, 100);
			// 					return false;
			// 				}
			// 			}
			// 		});
			// 	} else {
			// 		that.palletFocus = true;
			// 		that.oldFactoryId = e;
			// 		that.palletCode = null;
			// 	}
			// },
			// getList() {
			// 	that.$u.post('/wms/mobile/api/getFactories', {}).then(res => {
			// 		const list = res.data;
			// 		for (const item of list) {
			// 			item.text = item.name;
			// 			item.value = item.id;
			// 		}
			// 		that.factorieList = list;
			// 	});
			// },
			// confirmIn(e) {
			// 	// 根据仓位code查询库存
			// 	if (!this.scanSearchIn) {
			// 		return;
			// 	}

			// 	this.$u.get('/wms/mobile/api/getStockPalace', {
			// 		code: e,
			// 		factoryId: that.factoryId
			// 	}).then(res => {
			// 		if (res.code != 0) {
			// 			this.$refs.uToast.show({
			// 				title: res.message,
			// 				type: 'error'
			// 			});
			// 			return;
			// 		}
			// 		this.inObj = res.data;
			// 	});
			// },

			subAll() {
				if (that.formModel && that.formModel.length == 0) {
					that.$u.toast('请扫描箱码')
					that.setBoxFocus()
					return false
				}
				const param = {
					newPalletCode: that.palletCode,
					barcodeCartonNoSaveRequests: that.formModel
				}
				this.$u.post('/wms/mobile/api/palletOperationCartonNo', param).then(res => {
					that.formModel = []
					that.palletCode = null

					that.$u.toast('操作成功')
				})
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.content {
		padding-bottom: 180rpx;
	}

	.oneCard {
		padding: 25rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #dcdcdc;
	}

	.oneCard:last-child {
		border: none;
	}

	.btnRow {
		height: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.u-form-item {
		padding: 0 0;
	}

	.formStyle {
		display: flex;
		flex-flow: column;
		height: calc(100% - 80rpx);
	}

	.oneLit {
		padding: 0 0 0 20rpx;
	}

	.flexFull {
		flex: 1;
		overflow-y: auto;
	}

	.line-view {
		padding: 10rpx 0;
		font-size: 30rpx;
	}

	.addText {
		margin-left: 8rpx;
		color: #296ae2;
	}

	.btn-bottom-view-pallet {
		position: fixed;
		bottom: 0;
		width: 750rpx;
	}
</style>